<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class MedicalDispose extends Model
{
    protected $table = 'st_medical_dispose';

    protected $fillable = [
        'record_id', 'control_objectives', 'advise', 'inspection_plan', 'schedule', 'medication', 'cn_medication',
        'signatures', 'remark',
    ];

    protected $casts = [
        'control_objectives' => 'array',
        'advise'             => 'array',
        'inspection_plan'    => 'array',
        'schedule'           => 'array',
        'cn_medication'      => 'array',
        'medication'         => 'array',
    ];

    public function medicalRecord()
    {
        return $this->belongsTo(MedicalRecord::class, 'record_id');
    }

    // 获取处理西药用药
    public function medication($record_id)
    {
        $medication = static::where(['record_id' => $record_id])->first(['medication']);

        return $medication ? ($medication->medication ?: []) : [];
    }

    // 获取处理中药用药
    public function cnMedication($record_id)
    {
        $cnMedication = static::where(['record_id' => $record_id])->first(['cn_medication']);

        return $cnMedication ? ($cnMedication->cn_medication ?: []) : [];
    }
}
